# -*- coding: utf-8 -*-
from logger_setup import get_logger
import requests
import json
import yaml_tool
import net_tool

logout_logger = get_logger("logout")
try:
    cfg = yaml_tool.get_dict()
    net = cfg["net"]
    url = str(net["url"])
    userindex = str(net["userIndex"])
except Exception as e:
    logout_logger.warning('logout初始化失败')
    logout_logger.error(e)


def logout():
    """登出"""
    if not net_tool.is_network_port_connected():
        logout_logger.info('网络接口未连接，无法登出')
        return "网络接口未连接，无法登出"
    headers = {
        'Host': url[6:],
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0',
        'Referer': url + "/eportal/success.jsp?userIndex=" + userindex,
        'Connection': 'keep-alive',
        'Accept-Language': 'zh-CN,zh;q=0.9',
    }
    data = {
        "userIndex=": userindex
    }
    logout_url = url + "/eportal/InterFace.do?method=logout"
    logout_logger.info('logout数据整理完成')
    try:
        response = requests.post(url=logout_url, data=data, headers=headers)
        result1 = json.dumps(response.json(), ensure_ascii=False)
        result1 = result1.encode('ISO-8859-1')
        result1 = result1.decode('utf-8')
        logout_logger.info(result1)
        return result1
    except Exception as e:
        logout_logger.warning('发生错误')
        logout_logger.error(e)


